package com.atguigu.udmp.stream.common.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;


import java.sql.*;
import java.util.HashSet;
import java.util.Properties;

@Slf4j
public class DorisJdbcUtil {


    static Properties properties   = MyPropertiesUtil.load("config.properties");
    static  String  DORIS_URL = properties.getProperty("doris.url");
    static String DORIS_USERNAME = properties.getProperty("mysql.username");
    static String  DORIS_PASSWORD = properties.getProperty("mysql.password");



   /*
    查询某张表的分区
    */
    public static HashSet<String>  showPartition(String tableName   ) {
        HashSet<String> partitions = new HashSet<>();
        String sql ="show partitions from "+tableName;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw  new RuntimeException("找不到驱动");
        }
        try {
            Connection connection = DriverManager.getConnection(DORIS_URL,DORIS_USERNAME,DORIS_PASSWORD);
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()) {
                // 假设分片信息在第一列
                String partition = resultSet.getString(2);
                partitions.add(partition);
            }
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
            throw  new RuntimeException("数据库执行失败");
        }
        return partitions;

    }

    /*
     查询某张表的分区
     */
    public static  void addPartition(String tableName,String partitionName,String[] partitionValues){

        String sql ="alter table "+tableName+" add partition `"+partitionName+"` values in((\"" + StringUtils.join(partitionValues,"\",\"") +"\"))" ;
        log.info("尝试添加分区:"+sql);
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            throw  new RuntimeException("找不到驱动");
        }
        try {
            Connection connection = DriverManager.getConnection(DORIS_URL,DORIS_USERNAME,DORIS_PASSWORD);
            Statement statement = connection.createStatement();
             statement.execute (sql);
            connection.close();

        } catch (SQLException e) {
            if(e.getMessage().contains("Duplicate partition name")){
                log.warn("已存在分区:"+partitionName);
            }else {
                throw new RuntimeException("创建分区失败");
            }
        }

    }

    public static void main(String[] args) {
        System.out.println("DorisJdbcUtil.showPartition(\"udmp.user_event\") = " + DorisJdbcUtil.showPartition("udmp.user_event"));

        addPartition("udmp.user_event", "p_20231106_4", new String[]{"2023-11-06", "4"});

        System.out.println("DorisJdbcUtil.showPartition(\"udmp.user_event\") = " + DorisJdbcUtil.showPartition("udmp.user_event"));
        return;

    }
}
